package by.gamedev.site.client.app.place;

import com.google.gwt.activity.shared.FilteredActivityMapper;
import com.google.gwt.place.shared.Place;

/**
 * Converts a {@link #ProxyPlace} to a {@link ProxyListPlace}.
 */
public class ProxyPlaceToListPlace implements FilteredActivityMapper.Filter {

    /**
     * Required by {@link FilteredActivityMapper.Filter}, calls {@link #proxyListPlaceFor()}.
     */
    @Override
    public Place filter(final Place place) {
        return proxyListPlaceFor(place);
    }

    /**
     * @param place
     *            a place to process
     * @return an appropriate ProxyListPlace, or null if the given place has nothing to do with proxies
     */
    public ProxyListPlace proxyListPlaceFor(final Place place) {
        if(place instanceof ProxyListPlace ) {
            return (ProxyListPlace) place;
        }

        if(!(place instanceof ProxyPlace) ) {
            return null;
        }

        final ProxyPlace proxyPlace = (ProxyPlace) place;
        return new ProxyListPlace(proxyPlace.getProxyClass());
    }
}
